package top.dreamcenter.ai.llm.llmchat.config;

import io.modelcontextprotocol.client.McpClient;
import io.modelcontextprotocol.client.McpSyncClient;
import io.modelcontextprotocol.client.transport.HttpClientSseClientTransport;
import org.springframework.ai.mcp.SyncMcpToolCallbackProvider;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedList;
import java.util.List;

@Configuration
public class McpConfig {

    @Value("${spring.ai.mcp.client.enabled}")
    private boolean enableMcpClient;

    @Bean
    public SyncMcpToolCallbackProvider syncMcpToolCallbackProviders() {

        List<String> urlList = new LinkedList<>();
        urlList.add("http://localhost:30001");
        
        List<McpSyncClient> clients = new LinkedList<>();

        if (enableMcpClient){
            for (String url : urlList) {
                HttpClientSseClientTransport transport = new HttpClientSseClientTransport(url);
                try {
                    McpSyncClient client = McpClient.sync(transport).build();
                    if (client.listTools() != null) {
                        clients.add(client);
                    }
                } catch (Exception ignore){}
            }
        }


        return new SyncMcpToolCallbackProvider(clients);
    }

}
